﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Base;
using DHQGHN.DAL.BO.Admin;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.DAO.Admin
{
    public class UserInGroupDAO : SqlDAO
    {
        public const string SP_GET_LIST_PAGING_USER_IN_GROUP = "spGetListPagingUserInGroup";
        public const string SP_SEARCH_USERNAME = "spSearchUsername";
        public const string SP_INSERT_USER_INTO_GROUP = "insert_User_into_Group";
        public const string SP_INSERT_USER_TO_GROUP = "[spInsertUserIntoGroup]";
        public const string SP_GET_LIST_PAGING_USER_IN_GROUP_BY_ID = "spGetUserInGroupById";
        public const string SP_CREATE_OR_UPDATE_USER_IN_GROUP = "spCreateOrUpdateUserInGroup";
        public const string SP_DELETE_USER_IN_GROUP = "spDeleteUserInGroup";
        public const string Param_UserInGroupID = "@UserInGroupID";
        public const string Param_GroupID = "@GroupID";
        public const string Param_UserID = "@UserID";
        public const string Param_Groupname = "@Groupname";

        public const string Param_UserName = "@UserName";
        public const string Param_GroupCode = "@GroupCode";
        public const string Param_GroupCodeOld = "@GroupCodeOld";
        public const string Param_AppCode = "@AppCode";

        public const string Param_ID_OUT_IDOUT = "@IDOUT";
        public const string SP_GET_LIST_USER_OF_GROUP = "spGetListUserInGroupByGroupId";
        public const string SP_UPDATE_USER_IN_GROUP = "spUpdateUserInGroup";
        public const string SP_UPDATE_GROUP = "spUpdateGroupUserInGroup";

        public UserInGroupDAO(String connectionString, System.Type type = null)
            : base(connectionString, type) { ;}

        internal DataPage<UserInGroupModel> GetDynamicListPaging(UserInGroupModel model, int pageSize, int pageIndex, Dictionary<String, String> mapDictionary, bool compareEqual)
        {
            try
            {
                //SqlCommand command = SharedSqlDao.GetSprocCommand(pr_Person_GetAll);
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_USER_IN_GROUP);
                if (model != null)
                {
                    command.Parameters.Add(new SqlParameter(Param_UserInGroupID, model.UserInGroupId));
                    command.Parameters.Add(new SqlParameter(Param_UserID, model.UserId));
                    command.Parameters.Add(new SqlParameter(Param_GroupID, model.GroupId));
                }
                command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                return GetDataPage<UserInGroupModel>(command, pageIndex, pageSize, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal DataPage<UserInGroupModel> GetDynamicListPaging_PM2(UserInGroupModel model, int pageSize, int pageIndex, Dictionary<String, String> mapDictionary, bool compareEqual)
        {
            try
            {
                //SqlCommand command = SharedSqlDao.GetSprocCommand(pr_Person_GetAll);
                SqlCommand command = this.GetSprocCommand("spGetListPagingUserInGroup_PM2");
                if (model != null)
                {
                    command.Parameters.Add(new SqlParameter(Param_UserInGroupID, model.UserInGroupId));
                    command.Parameters.Add(new SqlParameter(Param_UserID, model.UserId));
                    command.Parameters.Add(new SqlParameter(Param_GroupID, model.GroupId));
                }
                command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                command.Parameters.Add(new SqlParameter("@DonViId", model.DonViId));

                return GetDataPage<UserInGroupModel>(command, pageIndex, pageSize, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal UserInGroupModel GetById(int? ID, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_USER_IN_GROUP_BY_ID);
                command.Parameters.Add(new SqlParameter(Param_UserInGroupID, ID));

                return GetSingle<UserInGroupModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal bool createOrUpdateUserInGroup(UserInGroupModel model, int[] listID)
        {
            try
            {
                foreach (int id in listID)
                {
                    SqlCommand command = this.GetSprocCommand(SP_CREATE_OR_UPDATE_USER_IN_GROUP);
                    command.Parameters.Add(new SqlParameter("@" + UserInGroupModel.FIELD_USER_IN_GROUP_ID, model.UserInGroupId));
                    command.Parameters.Add(new SqlParameter("@" + UserInGroupModel.FIELD_USER_ID, id));
                    command.Parameters.Add(new SqlParameter("@" + UserInGroupModel.FIELD_GROUP_ID, model.GroupId));
                    //command.Parameters.Add(new SqlParameter("@" + UserInGroupModel.FIELD_DESCRIPTION, model.Descriptions));
                    if (ExecuteQuery(command) <= 0)
                    {
                        return false;
                    }

                }
                return true;
            }
            catch
            {
                throw;
            }
        }

        internal bool deleteUserInGroup(int[] ID)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_DELETE_USER_IN_GROUP);
                command.Parameters.Add(new SqlParameter("@UserInGroupList", GetTableId<int>(ID, "UserInGroupId")));
                
                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        internal bool InsertUserIntoGroup(USER_GROUP_CREATE us)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_INSERT_USER_INTO_GROUP);
                command.Parameters.Add(new SqlParameter("@" + UserInGroupModel.FIELD_USER_NAME, us.UserName));
                command.Parameters.Add(new SqlParameter("@" + UserInGroupModel.FIELD_GROUP_CODE, us.GroupCode));
                
                return ExecuteNonQuery(command) == 0 ? false : true;
                //string output = GetSingleString(command);
                //if (output != null && !string.IsNullOrEmpty(output))
                //{
                //    return true;
                //}
                //return false;
            }
            catch
            {
                throw;
            }
        }

        internal bool AddUserIntoGroup(USER_GROUP_CREATE us)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_INSERT_USER_TO_GROUP);
                command.Parameters.Add(new SqlParameter(Param_UserName, us.UserName));
                command.Parameters.Add(new SqlParameter(Param_GroupCode, us.GroupCode));
                command.Parameters.Add(new SqlParameter(Param_AppCode, us.AppCode));
                int result = ExecuteNonQuery(command) ;

                return result > 0 ? true: false;
            }
            catch
            {
                throw;
            }
        }

        internal List<UserInGroupModel> GetListUserInGroupByGroupId(int? groupId, int? AppId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_USER_OF_GROUP);
                command.Parameters.Add(new SqlParameter(Param_GroupID, groupId));
                command.Parameters.Add(new SqlParameter("@AppId", AppId));
                return GetList<UserInGroupModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal bool UserInGroupUpdate(UserInGroupUpdate us)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_UPDATE_USER_IN_GROUP);
                //command.Parameters.Add(new SqlParameter(Param_UserName,us.UserName ));
                //command.Parameters.Add(new SqlParameter(Param_GroupCode, us.GroupCode));
                //command.Parameters.Add(new SqlParameter(Param_GroupCodeNew, us.GroupCodeNew));
                command.Parameters.Add(new SqlParameter("@" + UserInGroupModel.FIELD_USER_NAME, us.UserName));
                command.Parameters.Add(new SqlParameter("@" + UserInGroupModel.FIELD_GROUP_CODE, us.GroupCode));
                command.Parameters.Add(new SqlParameter("@" + UserInGroupModel.FIELD_GROUP_OLD_CODE, us.GroupCodeOld));
                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        internal bool UpdateGroup(UserInGroupUpdate data)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_UPDATE_GROUP);
                command .Parameters.Add(CreateParameter(Param_UserName,data.UserName));
                command.Parameters.Add(CreateParameter(Param_GroupCode, data.GroupCode));
                command.Parameters.Add(CreateParameter(Param_GroupCodeOld, data.GroupCodeOld));
                command.Parameters.Add(CreateParameter(Param_AppCode, data.AppCode));

                int result = ExecuteNonQuery(command);

                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }
    }
}
